// pages/mine/shop/mycard/cardnum.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    mycard_id: '',
    show: 1,
    hide: 0,
    tname: '选择充值套餐',
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    that.setData({
      mycard_id: options.id,
      token: extConfig.appid,
      show: 1,
      hide: 0,
      tname: '选择充值套餐',
    });
  },
  
  // 获取 会员卡的相关信息
  getCardMeal: function () {
    let that = this;
    app.ihttpGet('https://www.58zltc.com/api.php/MyCard/getMeal/openid/' + app.globalData.openid + '/card_id/' + that.data.mycard_id, function (res) {
      that.setData({
        mycard:res.data.mycard,
        meal:res.data.meal,
      })
      wx.hideLoading();
    });
  },

  bindChange: function (e) {
    let that = this;
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    console.log(e.detail.value);
    this.data.money = e.detail.value;
    this.data.recharge_id = 0;

  },
  radiochange: function (e) {
    var recharge_id = '';
    console.log(e.detail.value);
    this.data.meal_id = e.detail.value;

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    wx.showLoading({
      title: '加载中…',
    })
    app.ihttpGet('https://www.58zltc.com/api.php/ShopAdmin/getInfo/openid/' + app.globalData.openid, function (res) {
    
      that.setData({
        info: res.data,
        shop_id: res.data.shop_id,
      })

      wx.hideLoading();
    })
    this.getCardMeal();
  },

  cardMeal: function () {
    let that = this;
    var a = that.data.meal_id;
    console.log(a);
    if (!a) {
      wx.showToast({
        title: '请选择充次套餐!',
        icon: 'none',
        duration: 1500
      })
    } else {

      var post_data = {};
      post_data.openid = app.globalData.openid;
      post_data.meal_id = that.data.meal_id;//充值优惠记录id
      post_data.mycard_id = that.data.mycard_id;
      app.ihttpPost("https://www.58zltc.com/api.php/MyCard/meal", post_data, function (res) {
        if (res.data.err_code == 400) {
          app.iModel(res.data.err_msg);
        } else {
          var data = res.data;
          var prepay_id = res.data.package;
          var a = prepay_id.slice(10);
          wx.requestPayment({
            'appId': data.appId,
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': res => {
              //支付添加formid
              app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: a, openid: app.globalData.openid, type: 'pay' }, function (res) { })
              if (res.errMsg == 'requestPayment:ok') {
                wx.navigateBack({})
              }
            }
          })
        }
      })
    }
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})
